var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
var startX = 0, endX = 0, prevScrollLeft = 0, prevId = 0, prevIndex = 0;
var px = 0, py = 0, px2 = 0, py2 = 0, windowWidth = 0;
Page({
  data: {
    hasData: false,
    middle_height: "100vh",
    vehicle_data: [],
    detailShow: '',
    dd_style: "",
    path_data: [],
    searchShowed: false,
    searchVal: "",
    selected: false,
    vehicle_type: '0',
    tab: 4,
    filter: false,
    filterHeight: 0,
    pageSize: 10,
    pageNumber: 0,
    count: 0,
    page: 1,
    firstPage: true,
    prevPage: true,
    nextPage: true,
    lastPage: true,
    listSort:'DESC',
    sortText:'倒序',
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onShow: function (e) {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 10),
            warehouseId: res.data.value
          });
          that.loadData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--配送监控" });

    if(e.filter){
      that.setData({
        filter:true,
        filterHeight:52
      })
      if(e.filter =='carrier'){
        that.setData({
          carrier_coid: e.carrier_coid
        })
      }
    }
    that.setData({
      CO_ID:app.globalData.CO_ID
    })

    wx.showLoading({
      title: '加载中...',
    })
    if (app.globalData.userType == 'schedule') {
      that.setData({
        is_schedule: true
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 140 + that.data.filterHeight,
          detail_height: res.windowHeight - 40
        });
        windowWidth = res.windowWidth;
      }
    });
    if (e.warehouseId) {
      that.setData({
        warehouse: e.warehouse,
        warehouseId: e.warehouseId
      });
    }
    that.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName +'-配送监控',
      path: '/pages/schedule/monitor?warehouse=' + this.data.warehouse + '&warehouseId=' + this.data.warehouseId
    }
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.loadData();
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.loadData();
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.loadData();
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    let condition = ''
    if(that.data.searchVal){
      condition = "and ( plate like '%"+that.data.searchVal+"%')"
    }
    util.postData("/zhc/datalist/getDataByListId", {
      "list_id": "F905AEC8-5754-4B14-83E2-672E45717039",
      "page": {
        "size": that.data.pageSize,
        "current": that.data.page,
        "orders": [
          {
            "column": "a.ADD_TIME",
            "asc": "false"
          }
        ]
      },
      "data": {
        "field": ""
      },
      "repeat_field": [],
      "condition": " a.CO_ID='"+app.globalData.CO_ID+"' and  a.BE_DEL=0  "+condition,
      "virtual_field": []
    }, function (res, error) {
      console.log(res.data)
      if (res.data.success && res.data?.result?.records.length > 0) {
        var vehicle_data = res.data.result.records;
        var pageNumber = Math.ceil(res.data.result.total / that.data.pageSize);
        that.setData({
          hasData: true,
          vehicle_data: vehicle_data,
          count:res.data.result.total,
          pageNumber:pageNumber
        });
        that.setPageStatus();
        // var _iotbox = [], iot_index = 0, iot_data_index = 0;
        // for (var i = 0; i < vehicle_data.length;i++){
        //   if (vehicle_data[i].iot_box){
        //     iot_index++;
        //     util.getData("iotboxData", {
        //       iotbox: vehicle_data[i].iot_box
        //     }, function (data1, error) {
        //       var data1 = data1.data;
        //       //console.log(data1)
        //       var iot_box = data1.iot_box,longitude = data1.longitude, latitude = data1.latitude, gps_time = data1.gps_time, tp = data1.tp, hr = data1.hr;
        //       util.getData("coordToAddress", {
        //         longitude: longitude,
        //         latitude: latitude
        //       }, function (data2, error) {
        //         var data2 = data2.data;
        //         //console.log(data2)
        //         var address = data2.address;

        //         iot_data_index++;
        //         _iotbox[iot_box] = {
        //           addressOntime: address,
        //           tp:tp,
        //           hr:hr,
        //           gps_time: gps_time
        //         }
        //         if (iot_index == iot_data_index){
        //           var _vehicle_data = vehicle_data;
        //           for (var j = 0; j < _vehicle_data.length; j++) {
        //             if(_vehicle_data[j].iot_box){
        //               var _iotboxData = _iotbox[_vehicle_data[j].iot_box];
        //               //console.log(_iotboxData)
        //               _vehicle_data[j].addressOntime = _iotboxData.addressOntime;
        //               _vehicle_data[j].tp = _iotboxData.tp;
        //               _vehicle_data[j].hr = _iotboxData.hr;
        //               _vehicle_data[j].gps_time = util.dateObjectformat(new Date(_iotboxData.gps_time),'yyyy-MM-dd hh:mm');
        //             }
        //           }
        //           that.setData({
        //             vehicle_data: _vehicle_data
        //           });
        //         }
                
        //       });
        //     });
        //   }
        // }
      } else {
        that.setData({
          hasData: false,
          vehicle_data: []
        })
      };
      wx.hideLoading();
    });

  },
  //翻页按钮状态
  setPageStatus: function() {
    var that = this;
    var page = that.data.page,
      pageNumber = that.data.pageNumber;
    if (pageNumber == 1) {
      that.setData({
        firstPage: true,
        prevPage: true,
        nextPage: true,
        lastPage: true
      });
    } else if (pageNumber > 1) {
      if (page == 1) {
        that.setData({
          firstPage: true,
          prevPage: true,
          nextPage: false,
          lastPage: false
        });
      } else if (page == pageNumber) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: true,
          lastPage: true
        });
      } else if (page < pageNumber && page > 1) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: false,
          lastPage: false
        });
      }
    }
  },
  //分页按钮操作
  pageChange: function(e) {
    var type = e.currentTarget.dataset.page;
    if (type == 'first') {
      this.setData({
        page: 1
      })
      this.loadData();
    }
    if (type == 'last') {
      this.setData({
        page: this.data.pageNumber
      })
      this.loadData();
    }
    if (type == 'prev') {
      if (this.data.page > 1) {
        this.setData({
          page: this.data.page - 1
        })
        this.loadData();
      }
    }
    if (type == 'next') {
      if (this.data.page < this.data.pageNumber) {
        this.setData({
          page: this.data.page + 1
        })
        this.loadData();
      }
    }
  },
})
